// Loesung_von_Aufgabe_4.7_6_Generator

float w; // Winkel in Grad

void setup()
{
  size(400, 400, P3D);
}

void draw()
{
  background(255);

  translate(150, 200, 0); 

  // Winkelzunahme
  w = w + 1;

  pushMatrix();

  rotateX(radians(w));

  // Spule als 3D-Box
  noStroke();
  fill(#F7B407, 200);
  box(30, 200, 40); // In der Klammer stehen die Werte für x, y und z

  popMatrix();

  // Magnete
  stroke(0);
  strokeWeight(2);
  fill(255, 0, 0);
  rect(-30, -150, 60, 30);
  fill(0, 255, 0);
  rect(-30, 130, 60, 30);

  // Achse
  noStroke();
  fill(0);
  rect(-50, 0, 160, 8);

  // Schleifringe
  noStroke();
  fill(255, 0, 0);
  rect(50, -25, 45, 60);
  fill(0);
  rect(70, -25, 5, 60);

  // Leuchtdioden (noch nicht leuchtend)
  stroke(0);
  strokeWeight(4);
  noFill();
  triangle(130, 20, 150, -20, 170, 20);
  line(130, -20, 170, -20);
  triangle(180, -20, 200, 20, 220, -20);
  line(180, 20, 220, 20);

  // Leitungen
  noFill();
  beginShape();
  vertex(62, -23);
  vertex(62, -50);
  vertex(150, -50);
  vertex(150, -22);
  vertex(150, -22);
  vertex(150, -50);
  vertex(200, -50);
  vertex(200, -20);
  endShape();

  beginShape();
  vertex(81, 34);
  vertex(81, 50);
  vertex(150, 50);
  vertex(150, 22);
  vertex(150, 22);
  vertex(150, 50);
  vertex(200, 50);
  vertex(200, 20);
  endShape();

  float w2 = w%360; // Modulo-Operator

  // Farbe beim Aufleuchten der Leuchtdioden
  if (w2 >= 15 && w2 <= 165)
  {
    fill(0, 200, 0);
    triangle(130, 20, 150, -20, 170, 20);
  }

  if (w2 >= 185 && w2 <= 345)
  {
    fill(255, 0, 0);
    triangle(180, -20, 200, 20, 220, -20);
  }

  if (mousePressed)
  {
    saveFrame("Bilder/######.jpg");
  }
}